//
//  CustomNavigationBar.m
//  ifangchou
//
//  Created by cheng on 14-2-16.
//  Copyright (c) 2014年 fangchou. All rights reserved.
//

#import "CustomNavigationBar.h"
#import "UIColor+Util.h"
#import "ColorConfig.h"
#import "FontConfig.h"
#import "HorizonLineView.h"
#import "UIViewExt.h"

@interface CustomNavigationBar()<UIGestureRecognizerDelegate>

@property(strong,nonatomic) UIButton *leftButton;
@property(strong,nonatomic) UIButton *leftSecondButton;
@property(strong,nonatomic) UIImageView *leftImageView;
@property(strong,nonatomic) UILabel *leftLabel;
@property(strong,nonatomic) UILabel *middleLabel;
@property(strong,nonatomic) UIButton *rightButton;
@property(strong,nonatomic) UISegmentedControl *segmentedControl;
@property(strong,nonatomic) UIImageView *imageView;

//点击左按钮事件
-(void)clickLeftButtonEvent:(id)sender;
//点击右按钮事件
-(void)clickRightButtonEvent:(id)sender;
//segment选择事件
-(void)segmentAction:(id)sender;

@end

@implementation CustomNavigationBar

@synthesize leftButton;
@synthesize leftSecondButton;
@synthesize leftImageView;
@synthesize leftLabel;
@synthesize middleLabel;
@synthesize rightButton;
@synthesize segmentedControl;
@synthesize imageView;
@synthesize delegate;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.backgroundColor = MAIN_COLOR;
        [self createHorizonLine];
    }
    return self;
}

- (void)createHorizonLine
{
    HorizonLineView *line = [[HorizonLineView alloc] initWithFrame:CGRectMake(0.0f, self.height-1.0f, self.width, 1.0f)];
    line.backgroundColor = CUSTOM_LIGHT_GRAY_2;
    [self addSubview:line];

}


//添加标题
- (void)addNavigationBarTitile:(NSString*)title{
    
    if (!middleLabel) {
        
        float originX = 100.0f;
        float width = self.bounds.size.width - originX*2;
        middleLabel = [[UILabel alloc] initWithFrame:CGRectMake(originX, 25, width, 30)];
        [middleLabel setFont:SIZE_15];
        [middleLabel setText:title];
        [middleLabel setTextColor:[UIColor whiteColor]];
        [middleLabel setBackgroundColor:[UIColor clearColor]];
        middleLabel.textAlignment = NSTextAlignmentCenter;
        middleLabel.userInteractionEnabled = YES;
        [self addSubview:middleLabel];
    
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapMiddleLabelEvent:)];
        [middleLabel addGestureRecognizer:tap];
    }
}


-(void)modifyNavigationBarTtile:(NSString*)title
{
  if(!middleLabel)
  {
      [middleLabel setText:title];
  }
}

//添加左边文字表示
- (void)addNavigationBarLeftText:(NSString*)text{

    if (!leftLabel) {
        
        float originX = 30.0f;
        float width = 100.0f;
        leftLabel = [[UILabel alloc] initWithFrame:CGRectMake(originX, 25, width, 35)];
        [leftLabel setFont:SIZE_12];
        [leftLabel setTextColor:WHITE_COLOR];
        [leftLabel setBackgroundColor:[UIColor clearColor]];
        leftLabel.textAlignment = NSTextAlignmentLeft;
        leftLabel.userInteractionEnabled = YES;
        [self addSubview:leftLabel];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapLeftLabelEvent:)];
        [leftLabel addGestureRecognizer:tap];
    
    }
    
    [leftLabel setText:text];

}

//添加左按钮
- (void)addNavigationBarLeftButton:(NSString*)btnTitle{
    
    if (!leftButton) {
        CGFloat width = 60.0f;
        if (btnTitle.length > 0) {
            width = 80.0f;
        }
        leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 25, width, 35)];
        [leftButton addTarget:self action:@selector(clickLeftButtonEvent:) forControlEvents:UIControlEventTouchUpInside];
        [leftButton setTitle:btnTitle forState:UIControlStateNormal];
        [leftButton.titleLabel setFont:SIZE_15];
        [leftButton setTitleColor:WHITE_COLOR forState:UIControlStateNormal];
        [self addSubview:leftButton];
        //leftButton.backgroundColor = [UIColor redColor];
    }
}

//添加左边第二个按钮
- (void)addNavigationBarLeftSecondButton:(NSString*)btnTitle
{
    if (!leftSecondButton) {
        CGFloat originX = self.leftButton.left + self.leftButton.width - 10.0f;
        CGFloat width = 60.0f;
        leftSecondButton = [[UIButton alloc] initWithFrame:CGRectMake(originX, 25, width, 35)];
        [leftSecondButton addTarget:self action:@selector(clickedLeftSecondButtonEvent:) forControlEvents:UIControlEventTouchUpInside];
        [leftSecondButton setTitle:btnTitle forState:UIControlStateNormal];
        [leftSecondButton.titleLabel setFont:SIZE_12];
        [leftSecondButton setTitleColor:WHITE_COLOR forState:UIControlStateNormal];
        [self addSubview:leftSecondButton];
        //leftSecondButton.backgroundColor = [UIColor redColor];
    }
}

//添加右按钮
- (void)addNavigationBarRightButton:(NSString *)btnTitle{
    if (!rightButton) {
        float width = 65.0f;
        float originX = self.frame.size.width - width - 5.0f;
        rightButton = [[UIButton alloc] initWithFrame:CGRectMake(originX, 22, width, 30)];
        [rightButton addTarget:self action:@selector(clickRightButtonEvent:) forControlEvents:UIControlEventTouchUpInside];
        [rightButton setTitle:btnTitle forState:UIControlStateNormal];
        [rightButton setTitleColor:WHITE_COLOR forState:UIControlStateNormal];
        [rightButton.titleLabel setFont:SIZE_15];
        [self addSubview:rightButton];
    }
}

//添加中间segmentControl
-(void)addNavigationBarSegmentControl{
    
//    if (!segmentedControl) {
//        segmentedControl = [[UISegmentedControl alloc] initWithItems:
//                            [NSArray arrayWithObjects:
//                             nil]];
//        
//        segmentedControl.frame = CGRectMake(100, 10, 220, 30);
//        [segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
//        segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
//        segmentedControl.selectedSegmentIndex = 1;
//        [self addSubview:segmentedControl];
//    }
}

//添加中间ImageView
- (void)addNavigationBarImageView:(UIImage*)image
{
    float originX = (self.frame.size.width - image.size.width) / 2;
    CGRect rect = CGRectMake(originX, 10, image.size.width, image.size.height);
    imageView = [[UIImageView alloc] initWithFrame:rect];
    imageView.image = image;
    [self addSubview:self.imageView];

}

//添加左边图片
- (void)addNavigationBarLeftImageView:(UIImageView*)headerImageView
{
    [leftImageView removeFromSuperview];
    leftImageView = headerImageView;
    if (leftImageView) {
//        leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10.0f, 24.0f, 30.0f, 30.0f)];
        leftImageView.frame = CGRectMake(10.0f, 24.0f, 30.0f, 30.0f);
        leftImageView.layer.cornerRadius = 15.0f;
        leftImageView.layer.masksToBounds = YES;
        [self addSubview:leftImageView];
        leftImageView.userInteractionEnabled = YES;
        [self addSingleTagToView:leftImageView];
    }

}

- (void)setTitle:(NSString*)title
{
    if (self.middleLabel != nil) {
        self.middleLabel.text = title;
    }else{
        [self addNavigationBarTitile:title];
    }
}

- (void)setLeftButtonTitle:(NSString*)btnTitle
{
    if (self.leftButton != nil) {
        [self.leftButton setTitle:btnTitle forState:UIControlStateNormal];
    }

}

- (void)setNavigationBarLeftText:(NSString*)text
{
    if (self.leftLabel == nil) {
        [self addNavigationBarLeftText:text];
    }else{
        self.leftLabel.text = text;
    }
}

- (void)setRightButtonTitle:(NSString*)btnTitle
{
    if (self.rightButton != nil) {
        [self.rightButton setTitle:btnTitle forState:UIControlStateNormal];
    }
}

- (void)setLeftButtonBgImage:(UIImage*)image andHlImage:(UIImage*)hlImage
{
    if (leftButton == nil) {
        [self addNavigationBarLeftButton:nil];
    }
    [self.leftButton setImage:image forState:UIControlStateNormal];
    [self.leftButton setImage:hlImage forState:UIControlStateHighlighted];
    [self.leftButton setImageEdgeInsets:UIEdgeInsetsMake(0.0f, 0.0f, 0.0f, 30.0f)];

}


- (void)setRightButtonBgImage:(UIImage*)image andHlImage:(UIImage*)hlImage
{
    
    if (self.rightButton != nil) {
//        float width = image.size.width;
//        float height = image.size.height;
//        if (height > 26) {
//            float ration = 26 / height;
//            width = width * ration;
//            height = 26.0f;
//        }
//        float originY = 20.0f + (self.frame.size.height - 20.0f - height)/2 - 2.0f;
//        CGRect rect = CGRectMake(278, originY, width, height);
//        UIImageView *rightImageView = [[UIImageView alloc] initWithFrame:rect];
//        rightImageView.image = image;
        [self.rightButton setImage:image forState:UIControlStateNormal];
        [self.rightButton setImage:hlImage forState:UIControlStateHighlighted];
        [self.rightButton setImageEdgeInsets:UIEdgeInsetsMake(0.0f, 30.0f, 0.0f, 0.0f)];
    }
}

- (void)resetOriginXOfLeftLabel:(CGFloat)originX
{
    if (leftLabel != nil) {
        CGRect labelFrame = leftLabel.frame;
        labelFrame.origin.x = originX;
        leftLabel.frame = labelFrame;
    }
}

- (void)setLeftLabelColor:(UIColor*)textColor
{
    leftLabel.textColor = textColor;

}


//设置左按钮隐藏属性
-(void)setNavigationBarLeftButtonHidden:(BOOL)isHidden{
    
    if (leftButton != nil) {
        [leftButton setHidden:isHidden];
    }
}

-(void)setNavigationBarSecondBtnHidden:(BOOL)isHidden{
    if (leftSecondButton) {
        [leftSecondButton setHidden:isHidden];
    }
}

//点击左边标示文字事件
- (void)tapLeftLabelEvent:(id)sender
{
    [self clickLeftButtonEvent:sender];
}

//点击标题栏事件
- (void)tapMiddleLabelEvent:(id)sender
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(tapMiddleLabelEvent:)]) {
        [self.delegate tapMiddleLabelEvent:sender];
    }
}

//点击左按钮事件
-(void)clickLeftButtonEvent:(id)sender{
    
    if (self.delegate != nil && [self.delegate respondsToSelector:@selector(clickCustomNavigationBarLeftButtonEvent:)]) {
        [self.delegate clickCustomNavigationBarLeftButtonEvent:sender];
    }
    
}

//点击左边第二个按钮事件
- (void)clickedLeftSecondButtonEvent:(id)sender
{
    if (self.delegate != nil && [self.delegate respondsToSelector:@selector(clickCustomNavigationBarLeftSecondButtonEvent:)]) {
        [self.delegate clickCustomNavigationBarLeftSecondButtonEvent:sender];
    }
}

//点击右按钮事件
-(void)clickRightButtonEvent:(id)sender{
    
    if (self.delegate != nil && [self.delegate respondsToSelector:@selector(clickCustomNavigationBarRightButtonEvent:)]) {
        [self.delegate clickCustomNavigationBarRightButtonEvent:sender];
    }
}

//segment选择事件
- (void)segmentAction:(id)sender
{
    if (self.delegate != nil && [self.delegate respondsToSelector:@selector(customNavigationBarSegmentAction:)]) {
        [self.delegate customNavigationBarSegmentAction:sender];
    }
}

#pragma mark -

- (void)addSingleTagToView:(UIView*)view
{
    UITapGestureRecognizer *singleRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap:)];
    singleRecognizer.numberOfTapsRequired = 1; // 单击
    singleRecognizer.delegate = self;
    [view addGestureRecognizer:singleRecognizer];
    
    
}

#pragma mark -

-(void)singleTap:(UITapGestureRecognizer*)recognizer
{
    [self clickLeftButtonEvent:recognizer.view];
}


@end
